package com.bupt.secret.utils;

import com.bupt.secret.enums.BuptErrorCodeEnum;
import lombok.Getter;
import lombok.Setter;

/**
 * NodeJs响应实体
 */
@Getter
@Setter
public class BuptResultDto<T> {
    public static final int SUCCESS = 0;
    public static final int SIGN_CODE = 209;
    public static final int ERROR = 500;

    private int code;
    private String message;
    private T data;

    public BuptResultDto() {
        this.code = 200;
        this.message = "success";
    }

    public BuptResultDto(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public BuptResultDto(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> BuptResultDto<T> ok() {
        return restResult(null, SUCCESS, "操作成功");
    }

    public static <T> BuptResultDto<T> ok(T data) {
        return restResult(data, SUCCESS, "操作成功");
    }

    private static <T> BuptResultDto<T> restResult(T data, int code, String msg) {
        BuptResultDto<T> apiResult = new BuptResultDto<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMessage(msg);
        return apiResult;
    }

    public static <T>BuptResultDto error(int code) {
        BuptResultDto<T> apiResult = new BuptResultDto<>();
        apiResult.setCode(code);
        apiResult.setMessage(BuptErrorCodeEnum.fromCode(code).getDescription());
        return apiResult;
    }

    public static <T>BuptResultDto status(int code) {
        BuptResultDto<T> apiResult = new BuptResultDto<>();
        apiResult.setCode(code);
        apiResult.setMessage(BuptErrorCodeEnum.fromCode(code).getDescription());
        return apiResult;
    }

}
